package com.scrx.ymg.service.supermarket;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scrx.ymg.dao.agent.IAgentInfoDao;
import com.scrx.ymg.dao.direct.IDirectDao;
import com.scrx.ymg.dao.head.IHeadDao;
import com.scrx.ymg.dao.seckill.ISeckillDao;
import com.scrx.ymg.dao.supermarket.ISuperMarketDao;
import com.scrx.ymg.dao.util.IUtilDao;
import com.scrx.ymg.entity.AgentDrawings;
import com.scrx.ymg.entity.AgentHot;
import com.scrx.ymg.entity.AgentInfo;
import com.scrx.ymg.entity.AgentMoney;
import com.scrx.ymg.entity.BankAccount;
import com.scrx.ymg.entity.DirectMoney;
import com.scrx.ymg.entity.GoodsType;
import com.scrx.ymg.entity.HeadMoney;
import com.scrx.ymg.entity.HeadWithdarw;
import com.scrx.ymg.entity.OtherMoney;
import com.scrx.ymg.entity.SuperMarketEntrepot;
import com.scrx.ymg.entity.SuperMarketInfo;
import com.scrx.ymg.entity.SupermarketDrawings;
import com.scrx.ymg.entity.SupermarketMoney;
import com.scrx.ymg.entity.YmgHead;
import com.scrx.ymg.util.LatLonUtil;

@Service("superMarketService")
public class SuperMarketServiceImpl implements ISuperMarketService {

	@Autowired
	private ISuperMarketDao superMarketDao;
	@Autowired
	private IUtilDao utilDao;
	@Autowired
	private IHeadDao headDao;
	@Autowired
	private IAgentInfoDao agentDao;
	@Autowired
	private ISeckillDao seckilldao;
	@Autowired
	private IDirectDao directdao;
	
	

	/**
	 * 根据超市id查询超市所有商品
	 */
	public List<SuperMarketEntrepot> findAllByid(String superinfoID) {
		return superMarketDao.findAllbyID(superinfoID);
	}

	@Override
	public SuperMarketInfo findone(String username, String password) {

		return superMarketDao.findone(username, password);
	}

	public List<GoodsType> findAll() {
		return superMarketDao.findAll();
	}

	public List<GoodsType> findOne(String typeID) {
		return superMarketDao.findOne(typeID);
	}

	public List<SuperMarketInfo> findSuperListBylatlon(double maxlon, double maxlat, double minlon, double minlat) {

		return superMarketDao.findSuperListBylatlon(maxlon, maxlat, minlon, minlat);
	}

	public SuperMarketInfo findByID(Integer id) {

		return superMarketDao.findByID(id);
	}

	public Object loginByTypeID(String username, String password, String typeID) {
		Object obj = new Object();
		if (typeID.equals("0")) {// 总部
			obj = utilDao.getYmgHeadByPassWord(username, password);
		} else if (typeID.equals("1")) {// 代理
			obj = utilDao.getAgentInfoByPassWord(username, password);
		} else if (typeID.equals("2")) {// 异业
			obj = utilDao.getOtherInfoByPassWord(username, password);
		} else if (typeID.equals("3")) {// 超市
			obj = utilDao.getSuperMarketInfoByPassWord(username, password);
		}else if (typeID.equals("4")) {// 直营
			obj = utilDao.getDirectInfoByPassWord(username, password);
		} else {
			return null;
		}
		if (obj != null) {
			return obj;
		}
		return null;
	}

	public String modificationPassWord(int superId, String oldPassWord, String newPassWord) {
		SuperMarketInfo info = superMarketDao.findByID(superId);
		if (info != null && info.getPassword().equals(oldPassWord)) {
			info.setPassword(newPassWord);
			return superMarketDao.update(info);
		}
		return "false";
	}

	public SupermarketMoney getSupermarketMoneyInfo(int superId) {

		return superMarketDao.getSupermarketMoneyInfo(superId);
	}

	public String superMarketWithdraw(int superId) {
		// 1.得到超市营业额
		SupermarketMoney money = superMarketDao.getSupermarketMoneyInfo(superId);
		BankAccount bank = superMarketDao.isNullBankAccountInfo(superId);
		// 2.得到超市提现额度
		HeadWithdarw headwithdarw = headDao.getHeadWithdarw();
		double superWithdraw = headwithdarw.getSuperWithdraw();
		int poundage = headwithdarw.getWithdrawPoundage();
		double remaining = superWithdraw * poundage / 1000;
		double sum = superWithdraw + remaining;
		// 3.改变超市可提现金额
		if (money.getAccountMoney() > sum) {
			money.setAccountMoney(money.getAccountMoney() - sum);
			LatLonUtil util = new LatLonUtil();
			// 创建提现记录
			SupermarketDrawings drawings = new SupermarketDrawings();
			drawings.setWithdrawMoney(superWithdraw);
			drawings.setProcedureMoney(remaining);
			drawings.setSubmitTime(util.newdate());
			drawings.setWithdrawPeople(bank.getAccountName());
			drawings.setStatus(0);
			drawings.setTotalAmount(sum);
			drawings.setSuperId(superId);
			return superMarketDao.updateMoneyAndCreateLog(money, drawings);
		} else {
			return "可提现余额不能低于" + sum + "其中" + remaining + "为微信平台收取的手续费";
		}
	}

	public String createBankAccountInfo(BankAccount bankAccount) {
		BankAccount bank = null;
		if (bankAccount.getSuperId() != null) {
			bank = superMarketDao.isNullBankAccountInfo(bankAccount.getSuperId());
		} else if (bankAccount.getAgentId() != null) {
			bank = superMarketDao.isNullBankAccountInfoAgent(bankAccount.getAgentId());
		} else if (bankAccount.getOtherId() != null) {
			bank = superMarketDao.isNullBankAccountInfoOther(bankAccount.getOtherId());
		}else if (bankAccount.getDirectId() != null) {
			bank = superMarketDao.isNullBankAccountInfoDirect(bankAccount.getDirectId());
		}
		if (bank == null) {
			return superMarketDao.createBankAccountInfo(bankAccount);
		}
		return "250";

	}

	public String isNullBankAccountInfo(int superId) {
		BankAccount bank = superMarketDao.isNullBankAccountInfo(superId);
		if (bank != null) {
			return "true";
		}
		return "false";
	}

	public HeadWithdarw getHeadWithdarw() {
		return headDao.getHeadWithdarw();
	}

	public Map<Object, Object> getSupermarketDrawings(SupermarketDrawings drawing, int pageNum, int pageCount) {

		return superMarketDao.getSupermarketDrawings(drawing, pageNum, pageCount);
	}

	public Map<Object, Object> getAllSuperMarketInfo(SuperMarketInfo info, int pageNum, int pageCount) {

		return superMarketDao.getAllSuperMarketInfo(info, pageNum, pageCount);
	}

	public String freezeSuperMarket(SuperMarketInfo info) {
		SuperMarketInfo oldInfo = findByID(info.getId());
		oldInfo.setStatus(info.getStatus());
		oldInfo.setRemark(info.getRemark());
		return superMarketDao.update(oldInfo);
	}

	public String modificationPassWordByAgent(int agentId, String oldPassWord, String newPassWord) {
		AgentInfo info = agentDao.getAgentInfoByID(agentId);
		if (info != null && info.getPassword().equals(oldPassWord)) {
			info.setPassword(newPassWord);
			return agentDao.updateAgentInfo(info);
		}
		return "false";
	}

	public String isNullBankAccountInfoAgent(int agentId) {
		BankAccount bank = superMarketDao.isNullBankAccountInfoAgent(agentId);
		if (bank != null) {
			return "true";
		}
		return "false";
	}

	public String isNullBankAccountInfoOther(int otherId) {
		BankAccount bank = superMarketDao.isNullBankAccountInfoOther(otherId);
		if (bank != null) {
			return "true";
		}
		return "false";
	}

	public String AgentWithdraw(AgentDrawings drawing) {
		
		 HeadMoney headMoney = headDao.getHeadMoney();
		
		 
		if (drawing.getTypeId().equals(1)) {// 超市
			// 1.得到超市营业额
			SupermarketMoney money = superMarketDao.getSupermarketMoneyInfo(drawing.getMerchantId());
			BankAccount bank = superMarketDao.isNullBankAccountInfo(drawing.getMerchantId());
			// 2.得到超市提现额度
			HeadWithdarw headwithdarw = headDao.getHeadWithdarw();
			double superWithdraw = headwithdarw.getSuperWithdraw();
			int poundage = headwithdarw.getWithdrawPoundage();
			double remaining = superWithdraw * poundage / 1000;
			double sum = superWithdraw + remaining;
			// 3.改变超市可提现金额
			if (money.getAccountMoney() >= sum) {
				money.setAccountMoney(money.getAccountMoney() - sum);
				LatLonUtil util = new LatLonUtil();
				// 创建提现记录
				AgentDrawings drawings = new AgentDrawings();
				drawings.setWithdrawMoney(superWithdraw);
				drawings.setProcedureMoney(remaining);
				drawings.setSubmitTime(util.newdate());
				drawings.setWithdrawPeople(bank.getAccountName());
				drawings.setStatus(0);
				drawings.setTotalAmount(sum);
				drawings.setMerchantId(drawing.getMerchantId());
				drawings.setTypeId(drawing.getTypeId());
				headMoney.setTerraceAccountMoney(headMoney.getTerraceAccountMoney()-drawings.getTotalAmount());
				return superMarketDao.updateMoney(money, drawings,headMoney);
			} else {
				return "可提现余额不能低于" + sum + "其中" + remaining + "为微信平台收取的手续费";
			}

		} else if (drawing.getTypeId().equals(2)) {// 代理商
			// 1.得到代理商营业额
			AgentMoney agentMoney = agentDao.getAgentMoneyInfo(drawing.getMerchantId());
			BankAccount bank = superMarketDao.isNullBankAccountInfoAgent(drawing.getMerchantId());
			// 2.得到代理商提现额度
			HeadWithdarw headwithdarw = headDao.getHeadWithdarw();
			double agentWithdraw = headwithdarw.getAgentWithdraw();
			int poundage = headwithdarw.getWithdrawPoundage();
			double remaining = agentWithdraw * poundage / 1000;
			double sum = agentWithdraw + remaining;
			// 3.改变超市可提现金额
			if (agentMoney.getAccountMoney() >= sum) {
				agentMoney.setAccountMoney(agentMoney.getAccountMoney() - sum);
				LatLonUtil util = new LatLonUtil();
				// 创建提现记录
				AgentDrawings drawings = new AgentDrawings();
				drawings.setWithdrawMoney(agentWithdraw);
				drawings.setProcedureMoney(remaining);
				drawings.setSubmitTime(util.newdate());
				drawings.setWithdrawPeople(bank.getAccountName());
				drawings.setStatus(0);
				drawings.setTotalAmount(sum);
				drawings.setMerchantId(drawing.getMerchantId());
				drawings.setTypeId(drawing.getTypeId());
				headMoney.setTerraceAccountMoney(headMoney.getTerraceAccountMoney()-drawings.getTotalAmount());
				return superMarketDao.updateMoney(agentMoney, drawings,headMoney);
			} else {
				return "可提现余额不能低于" + sum + "其中" + remaining + "为微信平台收取的手续费";
			}

		} else if (drawing.getTypeId().equals(3)) {// 异业
			// 1.得到异业营业额
			OtherMoney agentMoney = seckilldao.getOtherMoney(drawing.getMerchantId());
			BankAccount bank = superMarketDao.isNullBankAccountInfoOther(drawing.getMerchantId());
			// 2.得到代理商提现额度
			HeadWithdarw headwithdarw = headDao.getHeadWithdarw();
			double agentWithdraw = headwithdarw.getOtherWithdraw();
			int poundage = headwithdarw.getWithdrawPoundage();
			double remaining = agentWithdraw * poundage / 1000;
			double sum = agentWithdraw + remaining;
			// 3.改变超市可提现金额
			if (agentMoney.getAccountMoney() >= sum) {
				agentMoney.setAccountMoney(agentMoney.getAccountMoney() - sum);
				LatLonUtil util = new LatLonUtil();
				// 创建提现记录
				AgentDrawings drawings = new AgentDrawings();
				drawings.setWithdrawMoney(agentWithdraw);
				drawings.setProcedureMoney(remaining);
				drawings.setSubmitTime(util.newdate());
				drawings.setWithdrawPeople(bank.getAccountName());
				drawings.setStatus(0);
				drawings.setTotalAmount(sum);
				drawings.setMerchantId(drawing.getMerchantId());
				drawings.setTypeId(drawing.getTypeId());
				headMoney.setTerraceAccountMoney(headMoney.getTerraceAccountMoney()-drawings.getTotalAmount());
				return superMarketDao.updateMoney(agentMoney, drawings,headMoney);
			} else {
				return "可提现余额不能低于" + sum + "其中" + remaining + "为微信平台收取的手续费";
			}
		} else if (drawing.getTypeId().equals(4)) {// 直营
			// 1.得到直营营业额	
			DirectMoney  directMoney =directdao.getDirectMoney(drawing.getMerchantId());
			
			BankAccount bank = superMarketDao.isNullBankAccountInfoDirect(drawing.getMerchantId());
			// 2.得到代理商提现额度
			HeadWithdarw headwithdarw = headDao.getHeadWithdarw();
			double dricetWithdraw = headwithdarw.getDricetWithdraw();
			int poundage = headwithdarw.getWithdrawPoundage();
			double remaining = dricetWithdraw * poundage / 1000;
			double sum = dricetWithdraw + remaining;
			// 3.改变超市可提现金额
			if (directMoney.getAccountMoney() >= sum) {
				directMoney.setAccountMoney(directMoney.getAccountMoney() - sum);
				LatLonUtil util = new LatLonUtil();
				// 创建提现记录
				AgentDrawings drawings = new AgentDrawings();
				drawings.setWithdrawMoney(dricetWithdraw);
				drawings.setProcedureMoney(remaining);
				drawings.setSubmitTime(util.newdate());
				drawings.setWithdrawPeople(bank.getAccountName());
				drawings.setStatus(0);
				drawings.setTotalAmount(sum);
				drawings.setMerchantId(drawing.getMerchantId());
				drawings.setTypeId(drawing.getTypeId());
				headMoney.setTerraceAccountMoney(headMoney.getTerraceAccountMoney()-drawings.getTotalAmount());
				return superMarketDao.updateMoney(directMoney, drawings,headMoney);
				//return superMarketDao.updateMoneyAndCreateLog(directMoney, drawings);
			} else {
				return "可提现余额不能低于" + sum + "其中" + remaining + "为微信平台收取的手续费";
			}
		}
		
		return "false";
	}

	public Map<Object, Object> getAgentDrawings(AgentDrawings drawing, int pageNum, int pageCount) {

		return superMarketDao.getAgentDrawings(drawing, pageNum, pageCount);
	}

	public String deleteSuperMarketInfo(SuperMarketInfo info) {
		SuperMarketInfo oldinfo = superMarketDao.findByID(info.getId());
		String msg = "";
		if (oldinfo != null) {
			msg = superMarketDao.deleteSuperMarketInfo(info);
		}
		return msg;
	}

	public Map<Object, Object> getSuperMarketInfoByStatus(SuperMarketInfo info, int pageNum, int pageCount) {
		return superMarketDao.getSuperMarketInfoByStatus(info, pageNum, pageCount);
	}


	public String checkSuperMarketInfo(SuperMarketInfo info) {
		SuperMarketInfo oldInfo = findByID(info.getId());
		oldInfo.setStatus(info.getStatus());
		oldInfo.setRemark(info.getRemark());
		SupermarketMoney money=new SupermarketMoney();
		money.setAccountMoney(0.0);
		money.setDirectMoney(0.0);
		money.setGroupMoney(0.0);
		money.setLocalMoney(0.0);
		money.setOnlineMoney(0.0);
		money.setSuperId(oldInfo.getId());
		money.setSuperMoney(0.0);
		money.setTotalMoney(0.0);
		List<AgentHot> hots=agentDao.getAgentHotsByAgentId(oldInfo.getAgentId());
		List<SuperMarketEntrepot> list=new ArrayList<SuperMarketEntrepot>();
		for (int i = 0; i < hots.size(); i++) {
			AgentHot agentHot=hots.get(i);
			SuperMarketEntrepot entre=new SuperMarketEntrepot();
			entre.setAdvertisement(agentHot.getAdvertisement());
			entre.setBarCode(agentHot.getBarCode());
			entre.setChecka(agentHot.getChecka());
			entre.setDiscountPrice(agentHot.getHotMoney());
			entre.setHot(1);
			entre.setInactive1(agentHot.getGoodstype());
			entre.setInactive3(agentHot.getNum());
			entre.setProductBimg(agentHot.getProductBimg());
			entre.setProductDetail(agentHot.getProductDetail());
			entre.setProductName(agentHot.getProductName());
			entre.setProductSimg(agentHot.getProductSimg());
			entre.setPutaway(1);//是否上架
			entre.setSales(0);
			entre.setSuperId(info.getId());
			entre.setRepertory(0);
			entre.setInactive3(agentHot.getNum());
			entre.setAgentHotId(agentHot.getId());
			entre.setSuperDetailImg(agentHot.getHotDetailImg());
			list.add(entre);
		}
		return superMarketDao.updateInfoAndSaveMoney(oldInfo,money, list);
	}
	
	public String isNullBankAccountInfoDirect(int directId) {
		BankAccount bank = superMarketDao.isNullBankAccountInfoDirect(directId);
		if (bank != null) {
			return "true";
		}
		return "false";
	}


	public String updatePassWord(String oldPassWord, String newPassWord) {
		YmgHead head = headDao.getHeadInfo();
		if(head.getPassWord().equals(oldPassWord)){
			head.setPassWord(newPassWord);
		}else{
			return "false";
		}
		return headDao.updateYmgHead(head);
	}

public AgentDrawings getAgentDrawings(int drawingId) {
		
		return superMarketDao.getAgentDrawings(drawingId);
	}

}
